﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Tasks.UI.Models.Account;
using Tasks.Business;
using Tasks.Common.Entitys;
using System.Collections;
using Tasks.Core;
using Tasks.Common;
using Tasks.UI.Models;
using System.Web.Script.Serialization;
using MyLib.Util;
using Tasks.Common.Enum;
using Tasks.API;

namespace Tasks.UI.Controllers
{
    /// <summary>
    /// 下拉框数据格式
    /// </summary>
    [Serializable]
    public class DropdownListXMLObject
    {
        //'UserID|Name|Linkman|LinkmanTel|MobileNo|Fax
        public string UserProfileID { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string IDCard { get; set; }

        public string Linkman { get; set; }
        public string LinkmanTel { get; set; }
        public string MobileNo { get; set; }
        public string Fax { get; set; }
    }

    public class DataController : BaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public ActionResult GetXmlData()
        {
            string dataType = Request["dataType"];
            string keyword = Request["keyword"];
            string dictKey = Request["dictKey"];
            int rowCount = 0;

            switch (dataType)
            {
                case "employee":
                    IList<Employee> employees = new AccountBLL().SearchEmployee(keyword, Config.CompanyID, 50, 1, out rowCount);
                    return new XmlResult(employees);
                case "dict":
                    IList<DictValue> dicts = new BasedataBLL(Config.CompanyID).GetDicts(dictKey);
                    return new XmlResult(dicts);
                case "enterpriseMember": //企业会员数据

                    rowCount = 15;
                    int ownerID1 = Config.CompanyID;

                    //企业拥有者
                    if (Request["OwnerID"] != null)
                    {
                        ownerID1 = int.Parse(Request["OwnerID"]);
                    }

                    IList<EnterpriseMember> eterprises;
                    if (Request["isTeamPlan"] != null && Convert.ToInt32(Request["isTeamPlan"]) == 1)
                    {
                        eterprises = new AccountBLL().GetEnterpriseMembers(keyword, new int[] { 1, 2, 3 }, ownerID1, rowCount);
                    }
                    else if (Request["EnterpriseType"] != null)
                    {
                        EnterpriseType entType = (EnterpriseType)int.Parse(Request["EnterpriseType"]);
                        eterprises = new AccountBLL().GetEnterpriseMembers(keyword, entType, ownerID1, rowCount);
                    }
                    else
                    {
                        //根据名称模糊查询
                        eterprises = new AccountBLL().GetEnterpriseMembers(keyword, ownerID1, rowCount);
                    }

                    return new XmlResult(eterprises);

               
            
                case "dept":
                    IList<DictValue> depts = BusinessFactory.BasedataBLL.GetDepartments();//部门
                    return new XmlResult(depts);
                case "categorys":
                    IList<DictValue> categorys = BusinessFactory.BasedataBLL.GetEnterpriseMemberCategorys();//企业分组
                    return new XmlResult(categorys);
                case "cats":
                    IList<DictValue> cats = BusinessFactory.BasedataBLL.Per_Member_Cat();//会员类别
                    return new XmlResult(cats);
                case "source":
                    IList<DictValue> source = BusinessFactory.BasedataBLL.Per_Member_Source();//客户来源
                    return new XmlResult(source);
            
            
             
              
            }
            return null;
        }

        /// <summary>
        /// 验证订单编码，用于POST机解码回调使用，仅提供给票务宝使用，需要验证票务宝的IP地址
        /// </summary>
        /// <param name="type">产品类型</param>
        /// <param name="orderNo">通过私钥加密后的订单号</param>
        /// <returns>
        /// 1 成功 -1 验证失败
        /// </returns>
        public ActionResult ValidateOrderCode(string type, string orderNo, string sign)
        {
            string logmessage = "type:" + type + "&orderNo=" + orderNo + "&sign=" + sign;
            ExecuteResult result = new ExecuteResult() { ResultNo = -1, Message = "数据填写不完整" };
            try
            {
                if (string.IsNullOrWhiteSpace(type) || string.IsNullOrWhiteSpace(orderNo))
                {
                    result = new ExecuteResult() { ResultNo = -1, Message = "数据填写不完整" };
                }
                else if (SecurityUtil.EncryptToMD5(orderNo + "ZYTX") != sign)
                    result = new ExecuteResult() { ResultNo = -1, Message = "签名不正确" };
                else
                {
                    ProductType productType = (ProductType)int.Parse(type);
                    switch (productType)
                    {

                    }
                }
            }
            catch (Exception ex)
            {

                Log.WriteLog(ex);
                result = new ExecuteResult() { ResultNo = -1, Message = "数据格式不正确" };
            }

            if (result.ResultNo != 1)
            {
                logmessage += "  errorMessage = " + result.Message;
                Log.WriteLog("datacontroller.ValidateOrderCode error, parm:" + logmessage);
            }
            else
            {
                return Content("success");
            }

            return new XmlResult(result);
        }

 

    }
}